package voting

import (
	"encoding/json"
	"errors"

	"awfan.cn/utils"
	"github.com/fatih/structs"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func edit(id string, w http.ResponseWriter, body []byte,db *mgo.Database)() {

	if id == "" {
		return nil, &utils.RESP_ERROR{404, errors.New("#id is empty")}
	}

	c := db.C(utils.PkgName(VOTINGS_XML{}))

	data := VOTING_XML{}

	err := json.Unmarshal(body, &data)

	if err != nil {
		return nil, &utils.RESP_ERROR{404, err}
	}

	m := structs.Map(data)

	delete(m, "Id")
	delete(m, "Created_time")

	err = c.UpdateId(bson.ObjectIdHex(id), bson.M{"$set": m})

	if err != nil {

		return nil, &utils.RESP_ERROR{404, err}
	}

	return "{}", nil
}
